package com.pap.msg.message.controller;

import com.pap.base.util.bean.BeanCopyUtilss;
import com.pap.beans.idworker.IdWorker;
import com.pap.msg.dto.MsgContentUserDetailsDTO;
import com.pap.msg.dto.MsgInfoDTO;
import com.pap.msg.dto.MsgInfoWithContentDTO;
import com.pap.msg.message.entity.MsgContent;
import com.pap.msg.message.entity.MsgInfo;
import com.pap.msg.message.service.IMsgContentService;
import com.pap.msg.message.service.IMsgInfoService;
import com.pap.obj.vo.logineduser.LoginedUser;
import com.pap.obj.vo.logineduser.LoginedUserVO;
import com.pap.obj.vo.page.Page;
import com.pap.obj.vo.response.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/msginfo")
@Api(value = "消息容器实体(发送接收关联关系)", tags = "消息容器实体(发送接收关联关系)", description="消息容器实体(发送接收关联关系)")
public class MsgInfoController {

	private static Logger logger  =  LoggerFactory.getLogger(MsgInfoController.class);
	
	@Resource(name = "msgInfoService")
	private IMsgInfoService msgInfoService;

	@Resource(name = "msgContentService")
	private IMsgContentService msgContentService;

	@Resource(name = "idWorker")
	private IdWorker idWorker;

	@ApiOperation("创建")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "msgInfoDTO", value = "", required = true, dataType = "MsgInfoDTO", paramType="body")
	})
	@PostMapping(value = "")
	public ResponseVO<MsgInfoDTO> create(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
										 @RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
										 @RequestBody MsgInfoDTO msgInfoDTO,
										 @LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		String idStr = idWorker.nextIdStr();
		msgInfoDTO.setMsgInfoId(idStr);

		MsgInfo databaseObj = new MsgInfo();
		BeanCopyUtilss.myCopyProperties(msgInfoDTO, databaseObj, loginedUserVO, true);

		int i = msgInfoService.insertSelective(databaseObj);

		return ResponseVO.successdata(msgInfoDTO);
	}

	@ApiOperation("更新")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "msgInfoDTO", value = "", required = true, dataType = "MsgInfoDTO", paramType="body")
	})
	@PutMapping(value = "")
	public ResponseVO<MsgInfoDTO> update(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@RequestBody MsgInfoDTO msgInfoDTO,
										 @LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){

		MsgInfo databaseObj = new MsgInfo();
		BeanCopyUtilss.myCopyProperties(msgInfoDTO, databaseObj, loginedUserVO, false);
		int i = msgInfoService.updateByPrimaryKeySelective(databaseObj);

		databaseObj = msgInfoService.selectByPrimaryKey(databaseObj.getMsgInfoId());

		return ResponseVO.successdata(databaseObj);
	}


	@ApiOperation("删除")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "msgInfoDTO", value = "", required = true, dataType = "MsgInfoDTO", paramType="body")
	})
	@DeleteMapping(value = "")
	public ResponseVO<String> delete(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@RequestBody MsgInfoDTO msgInfoDTO){

		int i = msgInfoService.deleteByPrimaryKey(msgInfoDTO.getMsgInfoId());

		return ResponseVO.successdata("操作成功");
	}

	@ApiOperation("详情")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "String", paramType="path")
	})
	@GetMapping(value = "/{id}")
	public ResponseVO<MsgInfoDTO> selectOne(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
													@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
													@PathVariable("id") String id){

		MsgInfo databaseObj = msgInfoService.selectByPrimaryKey(id);

		return ResponseVO.successdata(databaseObj);
	}

	@ApiOperation("查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
		@ApiImplicitParam(name = "msgInfoDTO", value = "应用查询参数", required = false, dataType = "MsgInfoDTO", paramType="body"),
		@ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int", paramType="query"),
		@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "int", paramType="query"),
		@ApiImplicitParam(name = "sortBy", value = "按照哪个属性排序", required = false, dataType = "String", paramType="query"),
		@ApiImplicitParam(name = "order", value = "排序顺序", required = false, dataType = "String", paramType="query")
	})
	@PostMapping(value = "/query")
	public ResponseVO<MsgInfoDTO>query(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
												@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
												@RequestBody MsgInfoDTO msgInfoDTO,
												@RequestParam Integer pageNo, @RequestParam Integer pageSize,
												@RequestParam(defaultValue = "create_time") String sortBy, @RequestParam(defaultValue = "desc") String order,
									  			 @LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		Map<Object, Object> map = BeanCopyUtilss.beanToMap(msgInfoDTO, loginedUserVO);
		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			map.put("page", newPage);
		}
		List<MsgInfo> list = msgInfoService.selectListByMap(map);

		//
		List<MsgInfoDTO> dtoList = toDTO(list);

		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			int countNum = msgInfoService.selectCountByMap(map);
			newPage.setCount(countNum);
			return ResponseVO.successdatas(dtoList, newPage);
		} else {
			return ResponseVO.successdatas(dtoList, null);
		}

	}


	@ApiOperation("查询用户实体消息(根据消息发送者/接收者/消息读取状态查询整个消息内容)")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "msgInfoDTO", value = "应用查询参数", required = false, dataType = "MsgInfoDTO", paramType="body"),
			@ApiImplicitParam(name = "pageNo", value = "页码", required = true, dataType = "int", paramType="query"),
			@ApiImplicitParam(name = "pageSize", value = "每页记录数", required = true, dataType = "int", paramType="query"),
			@ApiImplicitParam(name = "sortBy", value = "按照哪个属性排序", required = false, dataType = "String", paramType="query"),
			@ApiImplicitParam(name = "order", value = "排序顺序", required = false, dataType = "String", paramType="query")
	})
	@PostMapping(value = "/querycontent")
	public ResponseVO<MsgInfoWithContentDTO>queryContent(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
														 @RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
														 @RequestBody MsgInfoDTO msgInfoDTO,
														 @RequestParam Integer pageNo, @RequestParam Integer pageSize,
														 @RequestParam(defaultValue = "create_time") String sortBy, @RequestParam(defaultValue = "desc") String order,
														 @LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){
		Map<Object, Object> map = BeanCopyUtilss.beanToMap(msgInfoDTO, loginedUserVO);
		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			map.put("page", newPage);
		}
		List<MsgInfo> list = msgInfoService.selectListByMap(map);

		// 重新封装实体信息
		List<MsgInfoDTO> msgInfoDTOList = toDTO(list);

		List<MsgInfoWithContentDTO> msgInfoWithContentDTOList = new ArrayList<MsgInfoWithContentDTO>();
		if(msgInfoDTOList != null && msgInfoDTOList.size() > 0) {
			for(MsgInfoDTO msgInfoDTOTmp : msgInfoDTOList) {
				MsgInfoWithContentDTO msgInfoWithContentDTO = new MsgInfoWithContentDTO();
				BeanCopyUtilss.myCopyProperties(msgInfoDTOTmp, msgInfoWithContentDTO);

				MsgContent msgContent = msgContentService.selectByPrimaryKey(msgInfoDTOTmp.getMsgContentId());
				BeanCopyUtilss.myCopyProperties(msgContent, msgInfoWithContentDTO);

				msgInfoWithContentDTOList.add(msgInfoWithContentDTO);
			}
		}

		if(pageSize != 0) {
			Page newPage = new Page(pageNo + "", pageSize + "");
			int countNum = msgInfoService.selectCountByMap(map);
			newPage.setCount(countNum);
			return ResponseVO.successdatas(msgInfoWithContentDTOList, newPage);
		} else {
			return ResponseVO.successdatas(msgInfoWithContentDTOList, null);
		}

	}

	@ApiOperation("根据消息编号，将消息发送给指定用户集合")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "msgContentUserDetailsDTO", value = "消息用户关联对象", required = false, dataType = "MsgContentUserDetailsDTO", paramType="body")
	})
	@PostMapping(value = "/createmsginfobymsgcontentuserids")
	public ResponseVO<String> createMsgInfoByMsgContentUserIds(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
														 @RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
														 @RequestBody MsgContentUserDetailsDTO msgContentUserDetailsDTO,
														 @LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){

		msgInfoService.createMsgInfoByMsgContentUserIds(msgContentUserDetailsDTO, loginedUserVO);
		return ResponseVO.successdata("发送成功!");
	}


	@ApiOperation("根据当前用户信息，从消息实体中读取系统消息，将未同步过来的消息进行同步")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "papToken", value = "登录标识", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "papVersion", value = "版本号", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "userReceiverId", value = "消息接收者编号", required = true, dataType = "String", paramType="path")
	})
	@GetMapping(value = "/createsysmsgfrommsgcontent/{userReceiverId}")
	public ResponseVO<String> createSysMsgFromMsgContent(@RequestHeader(value = "papToken", defaultValue = "-1") String papToken,
											@RequestHeader(value = "papVersion", defaultValue = "1.0") String papVersion,
											@PathVariable("userReceiverId") String userReceiverId,
										 	@LoginedUser @ApiIgnore LoginedUserVO loginedUserVO){

		String msgInfoIds = msgInfoService.createSysMsgFromMsgContent(userReceiverId, loginedUserVO);

		return ResponseVO.successdata(msgInfoIds);
	}

	private List<MsgInfoDTO> toDTO(List<MsgInfo> databaseList) {
		List<MsgInfoDTO> returnList = new ArrayList<MsgInfoDTO>();
		if(databaseList != null) {
			for(MsgInfo dbEntity : databaseList) {
				MsgInfoDTO dtoTemp = new MsgInfoDTO();
				BeanCopyUtilss.myCopyProperties(dbEntity, dtoTemp);
				returnList.add(dtoTemp);
			}
		}
		return returnList;
	}

	private List<MsgInfo> toDB(List<MsgInfoDTO> dtoList) {
		List<MsgInfo> returnList = new ArrayList<MsgInfo>();
		if(dtoList != null) {
			for(MsgInfoDTO dtoEntity : dtoList) {
				MsgInfo dbTemp = new MsgInfo();
				BeanCopyUtilss.myCopyProperties(dtoEntity, dbTemp);
				returnList.add(dbTemp);
			}
		}
		return returnList;
	}
}
